package com.wechart.controller;


import com.wechart.common.Const;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by silenceholic on 2017/5/2.
 */
public class BaseController {

    /**
     * 得到request对象
     */
    public HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    protected ModelAndView modelAndView(Map<String, Object> map) {
        ModelAndView mv = new ModelAndView();
//        if (null != getUser()) {
//            mv.addObject("menu", ((Auth) getRequest().getSession().getAttribute(Const.SESSION_USER)).getMenu());
//            mv.addObject("username", getUser().getName());
//        }
//
//        mv.addObject("app", homeResource.getHomeUrl());
        if (null != getAuth()) {
            mv.addObject("rolePermission", ((Auth) getRequest().getSession().getAttribute(Const.SESSION_USER)).getUser().getRole());
            mv.addObject("areaPermission", ((Auth) getRequest().getSession().getAttribute(Const.SESSION_USER)).getUser().getAreacode());
        }

        if (null != map) {
            mv.addAllObjects(map);
        }
        return mv;
    }

    protected Auth getAuth() {
        Object auth = getRequest().getSession().getAttribute(Const.SESSION_USER);
        if (null == auth) {
            return null;
        } else {
            return ((Auth) getRequest().getSession().getAttribute(Const.SESSION_USER));
        }
    }
}
